/* 
 * File:   Process.h
 * Author: bharat
 *
 * Created on October 4, 2009, 11:54 AM
 */

#ifndef _PROCESS_H
#define	_PROCESS_H

#include "list.h"
#include "table.h"


class Thread;
class AddrSpace;

class Process {
private:
    int processId; // this is a processID = same as It's AddrSpace ID
    char* processName; // name if the process. same as the given executable.

    Table threadTable; //holds threads currently executing in the process.


    AddrSpace* addrSpace; // Addres-space for a process, all threads in this process shares this AddrSpace

    int exitCode;

    Lock* pLock;

    Table ConditionTable;
    Table LockTable;

public:
    int nChildren; // number of child threads running in this process.
    int numUserStacks; // keeps track of how many stacks the process has

    Process(AddrSpace* addressSpace, char* pName);

    void setAddressSpace(AddrSpace* sp);
    AddrSpace* getProcessAddressSpace();

    // this method is called on EXEC system call,
    // loads the executable contents into addressSpace and starts a new thread.
    int StartNewProcess();

    // this methos is called in FORK system call.
    // Creates a new thread and, assigns the addresspace of parent and calles the fork function.
    //@TODO: Change method for (VoidFunPointer) as a parameter.
    int addNewThread(unsigned int);

    int numberOfChildren() {
        return nChildren;
    }

    int getPID() {
        return processId;
    }

    void setPID(int pid) {
        processId = pid;
    }

    char* getPName() {
        return processName;
    }

    int getExitCode() {
        return exitCode;
    }

    int exitProcess(int exitCode);

    //lock related methods. They are called on various lock relarted system calls
    int CreateLock();
    int DestoryLock(int);
    int LockAcquire(int);
    int LockRelease(int);

    //condition related methods. They are called on various condition related system calls
    int CreateCondition();
    int DestroyCondition(int);
    int WaitCondition(int, int);
    int SignalCondition(int, int);
    int BroadcastCondition(int, int);

    //destrictor of the process class.
    ~Process();
};

#endif	/* _PROCESS_H */

